<!-- eslint-disable vue/valid-attribute-name -->
<template>
  <div>
    <!-- 顶部三级分类区域 -->
    <category :scene="scene"></category>
    <!-- SPU属性默认展示区域 -->
    <div v-show="scene == 0">
      <el-card style="margin: 10px 0px">
        <!-- 添加SPU按钮 -->
        <el-button
          type="primary"
          size="default"
          icon="Plus"
          :disabled="CategoryStore.c3Id ? false : true"
          @click="addSpu"
        >
          添加SPU
        </el-button>
        <!-- 添加SPU表格区域 -->
        <el-table style="margin: 10px 0px" border :data="records">
          <!-- 添加SPU表格内容区域 -->
          <!-- spu表格序号区域 -->
          <el-table-column
            label="序号"
            type="index"
            width="80px"
            align="center"
          ></el-table-column>
          <!-- spu表格名称区域 -->
          <el-table-column
            label="SPU名称"
            align="center"
            prop="spuName"
          ></el-table-column>
          <!-- spu表格描述区域 -->
          <el-table-column
            label="SPU描述"
            align="center"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <!-- spu表格操作区域 -->
          <el-table-column label="SPU操作" align="center">
            <!-- row:即为已有的SPU对象 -->
            <template #="{ row, $index }">
              <!-- 添加SKU -->
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加SKU"
                @click="addSku(row)"
              ></el-button>
              <!-- 修改SPU -->
              <el-button
                style="margin-left: 0 0 0 5px"
                type="success"
                size="small"
                icon="Edit"
                title="修改SPU"
                @click="updateSpu(row)"
              ></el-button>
              <!-- 查看SKU列表 -->
              <el-button
                type="warning"
                size="small"
                icon="View"
                title="查看SKU列表"
                @click="findSku(row)"
              ></el-button>
              <!-- 删除SKU -->
              <el-popconfirm
                :title="`您确定要删除${row.spuName}?`"
                width="250px"
                icon="QuestionFilled"
                @confirm="removeSpuName(row.id)"
              >
                <template #reference>
                  <el-button
                    type="primary"
                    size="small"
                    icon="Delete"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器区域 -->
        <!-- ->,sizes,total 将分页器这两个内容顶到最右边 -->
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @current-change="getHasSpu"
          @size-change="changeSize"
        />
      </el-card>
    </div>
    <!-- SPU属性切换展示区域 -->
    <!-- 添加和修改SPU 模块 -->
    <SpuForm ref="spu" v-show="scene == 1" @changeScene="changeScene"></SpuForm>
    <!-- 添加SkU 模块 -->
    <SkuForm ref="sku" v-show="scene == 2" @changeScene="changeScene"></SkuForm>
    <!-- sku 列表区域 -->
    <el-dialog v-model="show" title="sku列表">
      <!-- :data="skuArr 有问题 -->
      <el-table border :data="skuArr">
        <el-table-column label="sku名称" prop="skuName"></el-table-column>
        <el-table-column label="sku价格" prop="price"></el-table-column>
        <el-table-column label="sku重量" prop="weight"></el-table-column>
        <el-table-column label="sku图片">
          <template #="{ row, $index }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
// 引入element-plus信息提示
// import { ElMessage } from 'element-plus'
// 引入 api spu 请求
import { reqHasSpu, reqSkuInfo } from '@/api/product/spu/spu.ts'
// 引入接口 ts 类型
import type {
  HasSpuResponseData,
  Records,
  SpuData,
  SkuInfo,
  SkuData,
} from '@/api/product/spu/type'
// 获取分类的仓库
import useCategoryStore from '@/store/modules/category.ts'
// 引入子组件
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'
// 获取商品分类仓库
let CategoryStore = useCategoryStore()
// 控制场景的切换 0:显示已有SPU  1:添加或者修改已有SPU 2:添加SKU的结构
let scene = ref<number>(0)
// 分页器默认起始值页码
let pageNo = ref<number>(1)
// 每一页展示多少条数据
let pageSize = ref<number>(3)
// 存储已有SPU总个数
let total = ref<number>(0)
// 获取 spu 数据数组
let records = ref<Records>([])
//获取子组件实例SpuForm
let spu = ref<any>()
//获取子组件实例SpuForm
let sku = ref<any>()
// 存储全部的sku列表数据
let skuArr = ref<SkuData[]>([])
// 控制 sku 列表的显示与隐藏
let show = ref<boolean>(false)

// 监听三级分类ID的变化
watch(
  () => CategoryStore.c3Id,
  () => {
    //当三级分类发生变化的时候清空对应的数据
    records.value = []
    // 如果 spu 没有三级分类的ID 不执行数据请求
    if (!CategoryStore.c3Id) return
    // spu 数据请求
    getHasSpu()
  },
)
// spu 模块数据请求方法
const getHasSpu = async (pager = 1) => {
  // 修改当前页码
  pageNo.value = pager
  // 获取 spu 数据
  const result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    CategoryStore.c3Id,
  )
  if (result.code == 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}
// 分页器发生改变时触发
const changeSize = () => {
  getHasSpu()
}
// 点击添加 spu 属性按钮,切换场景
const addSpu = () => {
  scene.value = 1
  // 调用子组件方法，初始化数据
  spu.value.initAddSpu(CategoryStore.c3Id)
}
// 子组件SpuForm绑定自定义事件:目前是让子组件通知父组件切换场景为0 num: number
const changeScene = (obj: any) => {
  //子组件Spuform点击取消变为场景0:展示已有的SPU
  scene.value = obj.flag
  // 再次发请求，获取最新的spu数据
  if (obj.params == 'update') {
    // 如果是更改，留在当前页
    getHasSpu(pageNo.value)
  } else {
    // 添加回到第一页
    getHasSpu()
  }
}
// 点击修改按钮
const updateSpu = (row: SpuData) => {
  scene.value = 1
  spu.value.initHasSpuData(row)
}
// 点击添加sku按钮
const addSku = (row: SpuData) => {
  // 切换场景为2 添加sku
  scene.value = 2
  // 调用子组件方法 携带三级分类的一级，二级，三级的ID
  sku.value.initSkuData(CategoryStore.c1Id, CategoryStore.c2Id, row)
}
// 查看 sku 列表数据
const findSku = async (row: SpuData) => {
  const result: SkuInfo = await reqSkuInfo(row.id as number)
  console.log(result)
  console.log(result.data)
  if (result.code == 200) {
    // 存储sku列表数据
    skuArr.value = result.data
    // 展示sku列表数据 true
    show.value = true
  }
}
</script>

<style lang="scss" scoped></style>
